package com.rk.pethome.basic.util;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;

public class SendMsgUtil {
    private static final String UID="luolin123";
    private static final String KEY="d41d8cd98f00b204e980";

    /**
     * 发送短信
     * @param phone 发送的手机号码
     * @param text  发送的短信内容
     */
    public static  void sendCode(String phone,String text) {
        PostMethod post=null;
        try {
            //创建一个客户端
            HttpClient client = new HttpClient();
            //发送请求的url地址
            post = new PostMethod("http://utf8.api.smschinese.cn");
            //添加请求头信息
            post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");//在头文件中设置转码
            //准备请求体
            NameValuePair[] data = {new NameValuePair("Uid", UID), new NameValuePair("Key", KEY), new NameValuePair("smsMob", phone), new NameValuePair("smsText", text)};
            //设置请求体
            post.setRequestBody(data);
            //开始发送请求
            client.executeMethod(post);
  /*
            Header[] headers = post.getResponseHeaders();
            int statusCode = post.getStatusCode();
            System.out.println("statusCode:"+statusCode);
            for(Header h :headers)
            {
                System.out.println(h.toString());
            }
            String result = new String(post.getResponseBodyAsString().getBytes("utf8"));
            System.out.println(result); //打印返回消息状态*/
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            post.releaseConnection();
        }
    }
}